/*
 * SteepTimer - A timer designed for tea fanatics
 * Copyright (C) 2011  Wesley Crossman
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef TEAWINDOW_H
#define TEAWINDOW_H

#include <QMainWindow>
#include <QList>
#include <QHash>
#include <QTime>
#include <QModelIndex>
#include <QSessionManager>
#include <QSystemTrayIcon>
#include <QElapsedTimer>

namespace Ui {
    class TeaWindow;
}
class QAction;
class QLabel;
class QListWidgetItem;
class QMenu;
class QSettings;
class QSharedMemory;
class QTimer;
struct TeaIconSettings;

class GroupEntry : public QObject {
Q_OBJECT
public:
    explicit GroupEntry(QObject *parent);
    QString m_title, m_reminderNote;
    QList < QTime > m_timers;
};

class TeaWindow : public QMainWindow {
    Q_OBJECT

public:
    explicit TeaWindow(QWidget *parent = 0);
    ~TeaWindow();

    void setStartMinimized(bool value) {m_startMinimized = value;}

protected:
    void closeEvent(QCloseEvent *);

private:
    static const char *NEW_GROUP_COMBO;

    Ui::TeaWindow *m_ui;

    QSharedMemory *m_singleProcessMem;
    QTimer *m_singleProcessTimer;

    QLabel *m_lblGroupModified, *m_lblStatusTimeLeft;

    QSystemTrayIcon *m_trayIcon;
    QMenu *m_trayMenu;
    TeaIconSettings *m_iconSettings;
    QAction *m_aStartNext, *m_aStopTimer;
    bool m_startMinimized;

    QList < QTime > m_timers;
    QHash < QString, GroupEntry * > m_timerGroups;
    QString m_currentGroup;
    QTimer *m_clockCheckTimer, *m_updateUiTimer, *m_saveGroupsTimer;
    QElapsedTimer m_elapsed;
    bool m_timerGroupsModified;

    bool isTrayIconSet();

    int getCurrentIndex();
    QTime getCurrentTimer();
    int getNextTimerIndex();
    int getCurrentSecs();
    int getCurrentMsecs();
    int getRemainingSecs();
    QString convertTimeToString(const QTime &time);

private slots:
    void stopTimer();
    void startTimerByIndex(int);
    void startNextTimer();
    QListWidgetItem *addToList(int index);
    void repopulateList();

    void lazyLoad();
    void loadGroups(QSettings *settings);
    void populateComboBox();
    void saveGroups(QSettings *settings);
    bool isGroupChanged();
    void updateUi();
    void showEditDialog(int row);
    void setInTray(bool);

    void trayActivated(QSystemTrayIcon::ActivationReason);
    void singleProcessTimeout();
    void clockCheckTimeout();
    void saveGroupsTimeout();
    void aboutToQuitEvent();
    void commitData(QSessionManager &sessionManager);

    void on_btnStartStop_clicked();
    void on_btnNewTimer_clicked();
    void on_btnEditTimer_clicked();
    void on_btnDelGroup_clicked();
    void on_btnEditGroup_clicked();
    void on_btnSaveGroup_clicked();
    void on_cmbGroupNames_currentIndexChanged(const QString &text);
    void on_lstTimers_doubleClickedAnywhere(const QModelIndex &index);
    void on_aExit_triggered();
    void on_aSendToTray_triggered();
    void on_aImportTimerGroups_triggered();
    void on_aExportTimerGroups_triggered();
    void on_aOptions_triggered();
    void on_aContents_triggered();
    void on_aWebsite_triggered();
    void on_aAbout_triggered();
};

#endif // TEAWINDOW_H
